package com.yenlien.traceyun.control.controller;

import com.yenlien.traceyun.control.controller.form.FindStoreHouseDTO;
import com.yenlien.traceyun.control.dao.StoreManDao;
import com.yenlien.traceyun.control.entity.Position;
import com.yenlien.traceyun.control.entity.StoreHouse;
import com.yenlien.traceyun.control.entity.StoreMan;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.StoreHouseService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jialong
 * @date 2021/05/18
 * @description: 仓库
 **/
@RestController
@RequestMapping("/storeHouse")
public class StoreHouseController extends BaseController {

    @Autowired
    private StoreHouseService storeHouseService;

    @Autowired
    private StoreManDao storeManDao;


    @PostMapping(value = "/save")
    public Object save(@Validated @RequestBody StoreHouse storeHouse) {
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        storeHouse.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        List<Position> positionList = storeHouse.getPositionList();
        for (Position position : positionList) {
            position.setCompanyId(companyId);
        }
        return success(storeHouseService.save(storeHouse));
    }

    @PostMapping(value = "/findList")
    public Object findByList(@RequestBody StoreHouse storeHouse) {
        Page<StoreHouse> storeHousePage = storeHouseService.findList(storeHouse);
        if (storeHousePage.getContent() != null && storeHousePage.getContent().size() > 0) {
            for (StoreHouse storeHouse1 : storeHousePage.getContent()) {
                if (storeHouse1.getManageId() != null) {
                    storeHouse1.setManageName(storeManDao.findNameById(storeHouse1.getManageId()));
                }
            }
        }
        return buildPage(storeHousePage);
    }


    @GetMapping(value = "/findById/{id}")
    public Object findById(@PathVariable Long id) {
        return success(storeHouseService.findById(id));
    }

    @PostMapping(value = "/findChild")
    public Object findChild(@RequestBody FindStoreHouseDTO findStoreHouseDTO) {
        return success(storeHouseService.findChild(findStoreHouseDTO));
    }

    @PostMapping(value = "/storeMan")
    public Object addStoreMan(@RequestBody StoreMan storeMan) {
        storeMan.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return success(storeManDao.save(storeMan));
    }

    @GetMapping(value = "/listStoreMan")
    public Object listStoreMan() {
        return success(storeManDao.findByCompanyId(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @GetMapping(value = "/findManById")
    public Object findManById(Long id) {
        return success(storeManDao.findById(id));
    }

    @GetMapping(value = "/delManById")
    public Object delManById(Long id) {
        storeManDao.deleteById(id);
        return success("success");
    }

}
